﻿/***************************************************
//  Copyright (c) Premium Tax Free 2011
***************************************************/

using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using PremierTaxFree.PTFLib.Security;

namespace PremierTaxFree.PTFLib.Extensions
{
    public static class ByteArrayEx
    {
        public static byte[] Compress(this byte[] input)
        {
            Debug.Assert(input != null);
            Debug.Assert(input.Length > 0);

            using (MemoryStream memory = new MemoryStream())
            using (Stream gzip = new GZipStream(memory, CompressionMode.Compress))
            {
                gzip.Write(input, 0, input.Length);
                gzip.Close();
                return memory.ToArray();
            }
        }

        public static byte[] DeCompress(this byte[] input)
        {
            Debug.Assert(input != null);
            Debug.Assert(input.Length > 0);

            StringBuilder str = new StringBuilder();

            byte[] buffer = new byte[4096];

            using (MemoryStream memory = new MemoryStream(input))
            using (Stream gzip = new GZipStream(memory, CompressionMode.Decompress))
            {
                while (true)
                {
                    int size = gzip.Read(buffer, 0, buffer.Length);
                    if (size > 0)
                        str.Append(Encoding.Unicode.GetString(buffer, 0, size));
                    else
                        break;
                }
                gzip.Close();
            }
            return Encoding.Unicode.GetBytes(str.ToString());
        }

        public static ProtectedXml CreateXmlData(this byte[] buffer, ProtectionOptions options)
        {
            if (buffer == null || buffer.Length == 0)
                return null;

            ProtectedXml doc = new ProtectedXml(options);
            using (var memory = new MemoryStream(buffer))
            using (var reader = XmlReader.Create(memory))
                doc.Load(reader);
            return doc;
        }

        public static XmlDocument CreateXmlDoc(this byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0)
                return null;

            XmlDocument doc = new XmlDocument();
            using (var memory = new MemoryStream(buffer))
            using (var reader = XmlReader.Create(memory))
                doc.Load(reader);
            return doc;
        }

        public static XDocument CreateXDoc(this byte[] buffer)
        {
            if (buffer == null || buffer.Length == 0)
                return null;

            XDocument doc = null;
            using (var memory = new MemoryStream(buffer))
            using (var reader = XmlReader.Create(memory))
                doc = XDocument.Load(reader, LoadOptions.PreserveWhitespace);
            return doc;
        }
    }
}
